import { Application, Session } from "mydog";
import { DEF } from "../../../app/definition/consts";
import { gate_msg } from "../../../config/proto/gate";
import gateManager from "../../../app/gateManager";

export default class Handler {
  private app: Application;
  constructor(app: Application) {
    this.app = app;
  }

  /**
    * Gate handler that dispatch user to connectors
    * @param msg message from client
    * @param session
    */
  public connect(msg: gate_msg.c2s_connect, session: Session, next: Function) {
    const username = msg.username;

    const res: gate_msg.s2c_connect = new gate_msg.s2c_connect();

    if (!username) {
      res.code = DEF.SYSTEM.RETURN_CODE.ERROR;
      next(res);
      return res;
    }

    const connector = gateManager.getConnectorServer();
    res.code = DEF.SYSTEM.RETURN_CODE.SUCCESS;
    res.host = connector.host;
    res.port = connector.clientPort;
    next(res);
    return res;
  }
}
